/**
 * Copyright &copy; 2012-2016 <a href="https://istep.lczyfz.com">Istep</a> All rights reserved.
 */
package com.lczyfz.istep.modules.mobile.web;

import com.lczyfz.istep.common.config.Global;
import com.lczyfz.istep.common.persistence.Page;
import com.lczyfz.istep.common.utils.StringUtils;
import com.lczyfz.istep.common.web.BaseController;
import com.lczyfz.istep.modules.mobile.entity.Code;
import com.lczyfz.istep.modules.mobile.entity.MobileFile;
import com.lczyfz.istep.modules.mobile.entity.MobileMap;
import com.lczyfz.istep.modules.mobile.service.MobileFileService;
import com.lczyfz.istep.modules.mobile.service.MobileMapService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 移动双录文件管理Controller
 * @author dh
 * @version 2017-03-23
 */
@Controller
@RequestMapping(value = "${adminPath}/mobile/mobileFile")
public class MobileFileController extends BaseController {

	@Autowired
	private MobileFileService mobileFileService;
	@Autowired
	private MobileMapService mobileMapService;
	
	@ModelAttribute
	public MobileFile get(@RequestParam(required=false) String id) {
		MobileFile entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = mobileFileService.get(id);
		}
		if (entity == null){
			entity = new MobileFile();
		}
		return entity;
	}
	
	@RequiresPermissions("mobile:mobileFile:view")
	@RequestMapping(value = {"list", ""})
	public String list(MobileFile mobileFile, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<MobileFile> page = mobileFileService.findPage(new Page<MobileFile>(request, response), mobileFile); 
		model.addAttribute("page", page);
		return "modules/mobile/mobileFileList";
	}

	@RequiresPermissions("mobile:mobileFile:view")
	@RequestMapping(value = "form")
	public String form(MobileFile mobileFile, Model model) {
		model.addAttribute("mobileFile", mobileFile);
		return "modules/mobile/mobileFileForm";
	}

	@RequiresPermissions("mobile:mobileFile:edit")
	@RequestMapping(value = "save")
	public String save(MobileFile mobileFile, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, mobileFile)){
			return form(mobileFile, model);
		}
		mobileFileService.save(mobileFile);
		addMessage(redirectAttributes, "保存移动双录文件成功");
		return "redirect:"+Global.getAdminPath()+"/mobile/mobileFile/?repage";
	}
	
	@RequiresPermissions("mobile:mobileFile:edit")
	@RequestMapping(value = "delete")
	public String delete(MobileFile mobileFile, RedirectAttributes redirectAttributes) {
		mobileFileService.delete(mobileFile);
		addMessage(redirectAttributes, "删除移动双录文件成功");
		return "redirect:"+Global.getAdminPath()+"/mobile/mobileFile/?repage";
	}

	/**
	 * 解码
	 * @param mobileFile
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("mobile:mobileFile:edit")
	@RequestMapping(value = "decode")
	public String decode(MobileFile mobileFile, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, mobileFile)){
			return form(mobileFile, model);
		}
		MobileFile file = mobileFileService.get(mobileFile);
		Code code=new Code();
		try {
			code.decode(file);
		} catch (Exception e) {
			e.printStackTrace();
		}
		logger.debug("解密路径file=== [{}]", file.getUrlDecrypt());
		logger.debug("解密路径file=== [{}]", file.getUrlDecryptLocal());

		mobileFileService.save(file);
		addMessage(redirectAttributes, "解密成功");
		return "redirect:"+Global.getAdminPath()+"/mobile/mobileFile/?repage";
	}

	/**
	 * 播放视频
	 * @param mobileFile
	 * @param model
	 * @return
	 */
	@RequiresPermissions("mobile:mobileFile:view")
	@RequestMapping(value = "play")
	public String play(MobileFile mobileFile, Model model) {
		mobileFile=mobileFileService.get(mobileFile);
		model.addAttribute("mobileFile", mobileFile);
		logger.debug("解密路径file=== [{}]", mobileFile.getUrlDecrypt());

		return "modules/mobile/mobileFileForm";
	}

	/**
	 * 地图展示
	 *
	 * @param model
	 * @return
	 */
	@RequiresPermissions("mobile:mobileFile:view")
	@RequestMapping(value = "showMap")
	public String showMap(Model model) {
		List<MobileMap> mobileMapList = mobileMapService.findList(new MobileMap());
		List<List<String>> locationList = new ArrayList<>();
		for (MobileMap mp : mobileMapList) {
			Map<String, String> map = new HashMap<>();
			map.put(mp.getLongitude() + "," + mp.getLatitude(), mp.getFileName());
			List<String> mmm = new ArrayList<>();
			mmm.add(mp.getLongitude());
			mmm.add(mp.getLatitude());
			mmm.add("\'" + mp.getFileName() + "\'");
			locationList.add(mmm);
		}
		model.addAttribute("locationList", locationList);
		return "modules/mobile/mobileMap";
	}


}